package com.xiaoran.rabbitmq.demo.Produce;

import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//发送消息
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMsgController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //开始发送消息/ttl/sendMsg/
    @GetMapping("/sendMsg/{message}")
    public void getmessage(@PathVariable String message){
        System.out.println("发送条消息给两个队列："+message);
        rabbitTemplate.convertAndSend("X","XA","消息来自ttl为10s的队列："+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自ttl为40s的队列："+message);
    }

    //开始发送消息 消息TTL /ttl/sendTLL/你好1/20000
    @GetMapping("/sendTLL/{message}/{ttltime}")
    public void sendMsg(@PathVariable String message,@PathVariable String ttltime){
        System.out.println("发送条消息给一个队列QC："+message+"时长为："+ttltime);
        rabbitTemplate.convertAndSend("X","XC",message,msg->{
            //发送消息的时候设置 延迟时间
            msg.getMessageProperties().setExpiration(ttltime);
            return msg;
        });
    }

    //开始发送消息 基于延迟插件  /ttl/sendDEal
    @GetMapping("/sendDEal/{message}/{ttltime}")
    public void sendmmms(@PathVariable String message,@PathVariable int ttltime){
        System.out.println("发送条消息给一个队列："+message+"时长为："+ttltime);
        rabbitTemplate.convertAndSend("delayed.exchange","delayed.routingkey",message,msg->{
            //发送消息的时候设置延迟消息
            msg.getMessageProperties().setDelay(ttltime);
            return msg;
        });
    }



}
